package com.cuijiao.emp.java.controller;

import java.util.Date;
import java.util.Scanner;

import com.cuijiao.emp.java.data.EmpData;
import com.cuijiao.emp.java.model.Emp;
import com.cuijiao.emp.java.service.EmpService;
import com.cuijiao.emp.java.view.MainMenu;

/**
 * @author cuijiao
 *
 */
public class Controller {
    private MainMenu view = new MainMenu();

    private Scanner sc = new Scanner(System.in);

    private EmpService empService;

    public EmpService getEmpService() {
        return empService;
    }

    /**
     * 1. 通过setter方式注入service层依赖
     *
     * @param empService
     */
    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    /**
     * 2. 通过构造方法 注入service层依赖
     *
     * @param empService
     */
    public Controller(EmpService empService) {
        this.empService = empService;
    }

    public void start() {
        // TODO Auto-generated method stub
        // 测试数据
        // EmpData.empList.add(new Emp("lii", "001", 56666, false, new Date()));
        // EmpData.empList.add(new Emp("lucy", "0002", 88666, true, new Date()));
        // EmpData.empList.add(new Emp("mili", "002", 666, false, new Date()));
        // EmpData.empList.add(new Emp("hii", "003", 6600, true, new Date()));

        view.menu();

        int choice = -1;
        boolean flag = true;

        do {

            try {
                sc = new Scanner(System.in);
                choice = sc.nextInt();
                switch (choice) {
                case 0:
                    view.menu();
                    break;
                case 1:// 添加
                    empService.addEmp();
                    System.out.println("按0返回上一级菜单");
                    break;

                case 2:// 删除
                    empService.deleteEmp();
                    System.out.println("按0返回上一级菜单");
                    break;
                case 3:// 查找单个
                    empService.displayOne();
                    System.out.println("按0返回上一级菜单");
                    break;
                case 4:// 显示所有
                    empService.dispalayAll();
                    System.out.println("按0返回上一级菜单");
                    break;

                case 5:// 修改
                    empService.modifyEmp();
                    System.out.println("按0返回上一级菜单");
                    break;
                case 6:// 退出
                    flag = false;
                    System.out.println("谢谢使用");
                    break;
                default:
                    System.out.println("请输入1-6");
                    break;
                }
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("请输入数字！！！！");
                // nextInt()异常解决办法：1；2
                // sc = new Scanner(System.in);
                // sc.next();
            }
        } while (flag);
    }
}
